﻿using System.Collections.Generic;
using ProxyTool.Models;
using ProxyTool.Setting;
using ProxyTool.Utils;

namespace ProxyTool.Services
{
    /// <summary>
    ///     代理测试服务
    /// </summary>
    public static class ProxyTestService
    {
        /// <summary>
        ///     WSS代理测试地址
        /// </summary>
        public const string ProxyTestSvcURI = @"/wss/ProxyTest.svc";

        /// <summary>
        ///     代理测试服务地址
        /// </summary>
        public static string ProxyTestServiceURI
        {
            get
            {
                if (string.IsNullOrWhiteSpace(SettingManagement.Instance.Setting.U9URI))
                    throw new ProxyToolException("U9(C)地址 不能为空，请检查配置");
                return SettingManagement.Instance.Setting.U9URI + ProxyTestSvcURI;
            }
        }

        /// <summary>
        ///     查询服务
        /// </summary>
        public static List<BPSVType> QueryBPSV(string queryString)
        {
            const string serviceAction = "QueryBPSVType";
            const string serviceActionName = "查询代理服务";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            postData.Add("queryString", queryString);
            return ServiceHelper.DoRequest<List<BPSVType>>(ProxyTestServiceURI, serviceAction, serviceActionName,
                postData);
        }

        /// <summary>
        ///     通过BPSV类型对象获取代理类型对象
        /// </summary>
        public static string GetProxyType(BPSVType bpsvType)
        {
            const string serviceAction = "GetProxyType";
            const string serviceActionName = "通过BPSV类型对象获取代理类型对象";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            postData.Add("bpsvType", bpsvType);
            return ServiceHelper.DoRequest<string>(ProxyTestServiceURI, serviceAction, serviceActionName, postData);
        }

        /// <summary>
        ///     获取代理请求对象
        /// </summary>
        /// <param name="proxyType"></param>
        /// <param name="useDataContract"></param>
        /// <param name="requestDataMaxExpandDepth"></param>
        /// <returns></returns>
        public static ProxyData GetProxyData(string proxyType, bool useDataContract, int requestDataMaxExpandDepth)
        {
            const string serviceAction = "GetProxyData";
            const string serviceActionName = "获取代理请求对象";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            postData.Add("proxyType", proxyType);
            postData.Add("useDataContract", useDataContract);
            postData.Add("requestDataMaxExpandDepth", requestDataMaxExpandDepth);
            return ServiceHelper.DoRequest<ProxyData>(ProxyTestServiceURI, serviceAction, serviceActionName, postData);
        }

        /// <summary>
        ///     重新加载代理对象
        /// </summary>
        /// <param name="proxyData"></param>
        /// <returns></returns>
        public static ProxyData ReLoadProxy(ProxyData proxyData)
        {
            const string serviceAction = "ReLoadProxy";
            const string serviceActionName = "重新加载代理对象";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            postData.Add("proxyData", proxyData);
            return ServiceHelper.DoRequest<ProxyData>(ProxyTestServiceURI, serviceAction, serviceActionName, postData);
        }

        /// <summary>
        ///     执行代理请求
        /// </summary>
        /// <param name="proxyData"></param>
        /// <returns></returns>
        public static string ProxyTestDo(ProxyData proxyData)
        {
            const string serviceAction = "ProxyDo";
            const string serviceActionName = "执行代理请求";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            postData.Add("proxyData", proxyData);
            return ServiceHelper.DoRequest<string>(ProxyTestServiceURI, serviceAction, serviceActionName, postData);
        }
    }
}